package com.taotao.st3.base.mybatis.plugin;

import com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr;
import com.alibaba.druid.sql.ast.expr.SQLVariantRefExpr;
import com.alibaba.druid.sql.dialect.mysql.visitor.MySqlASTVisitorAdapter;
import lombok.Getter;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *
 * </p>
 *
 * @author fanbeibei
 * @version 1.0.0
 * @date 2023/6/14 16:32
 */
public class LikeParamIndexMySqlASTVisitor extends MySqlASTVisitorAdapter {

    @Getter
    private List<Integer> indexes = new ArrayList<>();


    private boolean visitLike = false;

    @Override
    public boolean visit(SQLVariantRefExpr x) {
        // 访问 ?
        if (visitLike) {
            System.out.println("add index " + x.getIndex());
            indexes.add(x.getIndex());
        }

        return true;
    }


    @Override
    public boolean visit(SQLBinaryOpExpr x) {

        if ("like".equalsIgnoreCase(x.getOperator().getName())) {
            System.out.println(" ***** like start " + x);

            visitLike = true;
        }


        return true;
    }


    @Override
    public void endVisit(SQLBinaryOpExpr x) {
        if ("like".equalsIgnoreCase(x.getOperator().getName())) {
            System.out.println(" ***** like end " + x);
            System.out.println();


            visitLike = false;
        }
    }
}
